package org.cainiao.io.bio;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class BioServer {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8080);
        System.out.println("Server listening on port 8080...");

        while (true) {
            Socket clientSocket = serverSocket.accept(); // 阻塞等待客户端连接
            System.out.println("Client connected.");

            BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));

            String request = reader.readLine(); // 阻塞等待客户端发送数据
            System.out.println("Received from client: " + request);

            String response = "Hello, Client!\n";
            writer.write(response);
            writer.flush();

            clientSocket.close();
            System.out.println("Client disconnected.");
        }
    }

}
